<template>
    <!-- 对话框表单 -->
    <!-- 建议使用 Prettier 格式化代码 -->
    <!-- el-form 内可以混用 el-form-item、FormItem、ba-input 等输入组件 -->
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="['Add', 'Edit'].includes(baTable.form.operate!)"
        @close="baTable.toggleForm"
        width="50%"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="config.layout.shrink ? '':'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    v-if="!baTable.form.loading"
                    ref="formRef"
                    @submit.prevent=""
                    @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items"
                    :label-position="config.layout.shrink ? 'top' : 'right'"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                >
                    <FormItem :label="t('order.service_id')" type="remoteSelect" v-model="baTable.form.items!.service_id" prop="service_id" :input-attr="{ pk: 'ba_.id', field: 'name', 'remote-url': '' }" :placeholder="t('Please select field', { field: t('order.service_id') })" />
                    <FormItem :label="t('order.user_id')" type="remoteSelect" v-model="baTable.form.items!.user_id" prop="user_id" :input-attr="{ pk: 'ba_.id', field: 'name', 'remote-url': '' }" :placeholder="t('Please select field', { field: t('order.user_id') })" />
                    <FormItem :label="t('order.technician_id')" type="remoteSelect" v-model="baTable.form.items!.technician_id" prop="technician_id" :input-attr="{ pk: 'ba_.id', field: 'name', 'remote-url': '' }" :placeholder="t('Please select field', { field: t('order.technician_id') })" />
                    <FormItem :label="t('order.service_start_time')" type="datetime" v-model="baTable.form.items!.service_start_time" prop="service_start_time" :placeholder="t('Please select field', { field: t('order.service_start_time') })" />
                    <FormItem :label="t('order.fare_price')" type="number" prop="fare_price" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.fare_price" :placeholder="t('Please input field', { field: t('order.fare_price') })" />
                    <FormItem :label="t('order.fare_price_imgs')" type="string" v-model="baTable.form.items!.fare_price_imgs" prop="fare_price_imgs" :placeholder="t('Please input field', { field: t('order.fare_price_imgs') })" />
                    <FormItem :label="t('order.fare_is_pay')" type="number" prop="fare_is_pay" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.fare_is_pay" :placeholder="t('Please input field', { field: t('order.fare_is_pay') })" />
                    <FormItem :label="t('order.service_price')" type="number" prop="service_price" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.service_price" :placeholder="t('Please input field', { field: t('order.service_price') })" />
                    <FormItem :label="t('order.service_total_price')" type="number" prop="service_total_price" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.service_total_price" :placeholder="t('Please input field', { field: t('order.service_total_price') })" />
                    <FormItem :label="t('order.service_num')" type="number" prop="service_num" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.service_num" :placeholder="t('Please input field', { field: t('order.service_num') })" />
                    <FormItem :label="t('order.service_reality_price')" type="number" prop="service_reality_price" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.service_reality_price" :placeholder="t('Please input field', { field: t('order.service_reality_price') })" />
                    <FormItem :label="t('order.service_duration')" type="number" prop="service_duration" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.service_duration" :placeholder="t('Please input field', { field: t('order.service_duration') })" />
                    <FormItem :label="t('order.refund_price')" type="number" prop="refund_price" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.refund_price" :placeholder="t('Please input field', { field: t('order.refund_price') })" />
                    <FormItem :label="t('order.order_sn')" type="string" v-model="baTable.form.items!.order_sn" prop="order_sn" :placeholder="t('Please input field', { field: t('order.order_sn') })" />
                    <FormItem :label="t('order.payment_order_sn')" type="string" v-model="baTable.form.items!.payment_order_sn" prop="payment_order_sn" :placeholder="t('Please input field', { field: t('order.payment_order_sn') })" />
                    <FormItem :label="t('order.payment_time')" type="datetime" v-model="baTable.form.items!.payment_time" prop="payment_time" :placeholder="t('Please select field', { field: t('order.payment_time') })" />
                    <FormItem :label="t('order.pay_way')" type="radio" v-model="baTable.form.items!.pay_way" prop="pay_way" :data="{ content: { ALIPAY_APP: 'pay_way ALIPAY_APP', WECHAT_APP: 'pay_way WECHAT_APP', WECHAT_MINI: 'pay_way WECHAT_MINI', WECHAT_JSAPI: 'pay_way WECHAT_JSAPI', WECHAT_WEB: 'pay_way WECHAT_WEB' } }" :placeholder="t('Please select field', { field: t('order.pay_way') })" />
                    <FormItem :label="t('order.order_state')" type="radio" v-model="baTable.form.items!.order_state" prop="order_state" :data="{ content: { ToBePaid: 'order_state ToBePaid', Paid: 'order_state Paid', SetOut: 'order_state SetOut', Reach: 'order_state Reach', Completed: 'order_state Completed', Canceled: 'order_state Canceled' } }" :placeholder="t('Please select field', { field: t('order.order_state') })" />
                    <FormItem :label="t('order.evaluate_state')" type="radio" v-model="baTable.form.items!.evaluate_state" prop="evaluate_state" :data="{ content: { NULL: 'evaluate_state NULL', ToBeEvaluated: 'evaluate_state ToBeEvaluated', Reviewed: 'evaluate_state Reviewed' } }" :placeholder="t('Please select field', { field: t('order.evaluate_state') })" />
                    <FormItem :label="t('order.address_username')" type="string" v-model="baTable.form.items!.address_username" prop="address_username" :placeholder="t('Please input field', { field: t('order.address_username') })" />
                    <FormItem :label="t('order.address_tel')" type="string" v-model="baTable.form.items!.address_tel" prop="address_tel" :placeholder="t('Please input field', { field: t('order.address_tel') })" />
                    <FormItem :label="t('order.address_house')" type="string" v-model="baTable.form.items!.address_house" prop="address_house" :placeholder="t('Please input field', { field: t('order.address_house') })" />
                    <FormItem :label="t('order.address_content')" type="string" v-model="baTable.form.items!.address_content" prop="address_content" :placeholder="t('Please input field', { field: t('order.address_content') })" />
                    <FormItem :label="t('order.technician_address')" type="string" v-model="baTable.form.items!.technician_address" prop="technician_address" :placeholder="t('Please input field', { field: t('order.technician_address') })" />
                    <FormItem :label="t('order.technician_tel')" type="string" v-model="baTable.form.items!.technician_tel" prop="technician_tel" :placeholder="t('Please input field', { field: t('order.technician_tel') })" />
                    <FormItem :label="t('order.remarks')" type="string" v-model="baTable.form.items!.remarks" prop="remarks" :placeholder="t('Please input field', { field: t('order.remarks') })" />
                    <FormItem :label="t('order.type')" type="radio" v-model="baTable.form.items!.type" prop="type" :data="{ content: {} }" :placeholder="t('Please select field', { field: t('order.type') })" />
                    <FormItem :label="t('order.association_order_sn')" type="string" v-model="baTable.form.items!.association_order_sn" prop="association_order_sn" :placeholder="t('Please input field', { field: t('order.association_order_sn') })" />
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm()">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)" type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import type { FormInstance, FormItemRule } from 'element-plus'
import { inject, reactive, ref } from 'vue'
import { useI18n } from 'vue-i18n'
import FormItem from '/@/components/formItem/index.vue'
import { useConfig } from '/@/stores/config'
import type baTableClass from '/@/utils/baTable'
import { buildValidatorData } from '/@/utils/validate'

const config = useConfig()
const formRef = ref<FormInstance>()
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    service_start_time: [buildValidatorData({ name: 'date', title: t('order.service_start_time') })],
    fare_price: [buildValidatorData({ name: 'number', title: t('order.fare_price') })],
    fare_is_pay: [buildValidatorData({ name: 'number', title: t('order.fare_is_pay') })],
    service_price: [buildValidatorData({ name: 'number', title: t('order.service_price') })],
    service_total_price: [buildValidatorData({ name: 'number', title: t('order.service_total_price') })],
    service_num: [buildValidatorData({ name: 'number', title: t('order.service_num') })],
    service_reality_price: [buildValidatorData({ name: 'number', title: t('order.service_reality_price') })],
    service_duration: [buildValidatorData({ name: 'number', title: t('order.service_duration') })],
    refund_price: [buildValidatorData({ name: 'number', title: t('order.refund_price') })],
    payment_time: [buildValidatorData({ name: 'date', title: t('order.payment_time') })],
    create_time: [buildValidatorData({ name: 'date', title: t('order.create_time') })],
})
</script>

<style scoped lang="scss"></style>
